﻿/*
* Copyright (C) 2019 Tianjin KYLIN Information Technology Co., Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/&gt;.
*
*/

#ifndef SINGLEMSG_H
#define SINGLEMSG_H

#include <QWidget>
#include <QLabel>
#include <QDateTime>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QStyleOption>
#include <QPixmap>
#include <QPainter>
#include <QPropertyAnimation>
#include <QDebug>
#include <QTimer>
#include <QThread>
#include <QPixmap>
#include "appmsg.h"
#include "../gsettingmanager.h"
#include "../dataprocess.h"
#include "../commondata.h"

#define TWENTY_FOUR_HOUR_SYSTEM true
#define TWELVE_HOUR_SYSTEM      false

#define CLICK_MESSAGEBAR_BODY "click MessageBar body"
#define MESSAGEBAR            "MessageBar - "
#define DELETE                "Delete"

extern void qt_blurImage(QImage &blurImage, qreal radius, bool quality, int transposed);

class AppMsg;
class SingleWidget;

class SingleMsg : public QWidget
{
    Q_OBJECT
public:
    SingleMsg(AppMsg* pParent, QString strIconPath, QString strAppName, QString strSummary, QDateTime dateTime, QString strBody, QString strUrl, QString strAction);
    ~SingleMsg();

    void updatePushTime();                                   //刷新时间
    void setBodyLabelWordWrap(bool bFlag);                   //设置正文显示状态，省略显示或全部展示
    void setLeftItem(int nShowLeftCount);                    //将应用的总的剩余条数设置剩余标签显示内容,根据具体情况显示,该函数只有顶部消息才执行
    void setMainFlag(bool bFlag) {m_bMain = bFlag;}          //设置主消息类别
    void setFoldFlag(bool bFlag);                            //设置折叠状态
    void setShowLeftItemFlag(bool bFlag);                    //设置应用未展开条数状态，隐藏或显示
    void mainMsgSetShowOrFold(bool bFlag);                   //设置通知展开或折叠
    void startAnimationUnfold();                             //开启展开动画
    void startAnimationFold();                               //开启折叠动画
    void setSingleMsgContentsMargins(int left, int top, int right, int bottom);    //折叠时设置间隙

    uint        getPushTime() {return m_uNotifyTime;}     //获取消息绝对时间的接口
    QDateTime   getPushDateTime() {return m_dateTime;}    //获取消息推送时间接口
    QString     getIcon() {return m_strIconPath;}         //获取消息图标路径接口
    QString     getSummary() {return m_strSummary;}       //获取消息主题接口
    QString     getBody() {return m_strBody;}             //获取消息正文接口

protected:
    virtual void enterEvent(QEvent *event) override;
    virtual void leaveEvent(QEvent *event) override;
    virtual void mousePressEvent(QMouseEvent *event) override;
    virtual void mouseReleaseEvent(QMouseEvent *event) override;
    virtual void mouseMoveEvent(QMouseEvent *event) override;

private:
    void initUI();                                           //初始化UI以及布局
    void initAttribute();                                    //初始化界面属性
    void initLayout();                                       //初始化布局
    void initJumpConnect();                                  //初始化连接跳转动作
    void startAnimationDeleUpperMove();                      //开启删除上移动画
    void initGsetting();                                     //初始化gsetting
    void setLabelAlphaF(QFrame *this_label, qreal alpha);    //设置文本透明度
    void jumpAction();                                       //执行跳转动作
    QString changeSummaryLabelSize(QLabel *target);


    AppMsg*         m_pParent;                      //父类指针
    QVBoxLayout*    m_pAppVLaout;                   //整体布局
    QHBoxLayout*    m_pIconHLayout;                 //图标布局
    QVBoxLayout*    m_pMainVLaout;                  //单条消息总体垂直布局器
    QHBoxLayout*    m_pTimeLableHLayout;            //设置标签布局
    QHBoxLayout*    m_pStorageDeleteButtonHLaout;   //收纳和删除按钮布局
    QVBoxLayout*    m_pVContextLayout;              //内容部件的垂直布局器
    SingleWidget*   m_pSingleWidget;                //除去底部6px的空白之外的一个内框部件
    QWidget*        m_pAnimationBaseMapWidget;      //动画底图动态模拟大小替身部件
    QWidget*        m_pIconWidget;                  //在动画中暂定高38px
    QWidget*        m_pTimeLabelWidget;             //时间窗体
    QWidget*        m_pStorageDeleteButtonWidget;   //删除按钮

    QLabel*         m_pTimeLabel;                   //时间标签
    QLabel*         m_pIconToolButton;              //通知消息中的Icon
    QPushButton*    m_pSingleDeleteButton;          //单条消息中的删除按钮
    QLabel*         m_pSummaryLabel;                //通知消息主题
//    QLabel*         m_pBodyLabel;                   //正文标签,收缩时,在动画中暂定高24px
    QTextBrowser*   m_pBodyLabel;
    QLabel*         m_pAppNameLabel;                //发出消息的APP名称
    QWidget*        m_pShowLeftWidget;              //显示该应用未展开部件
    QLabel*         m_pShowLeftItemLabel;           //显示该应用未展开条数

    QWidget*        m_pContextWidget;               //消息文字内容

    QString         m_strIconPath;                  //图标路径
    QString         m_strAppName;                   //保存程序名称
    QString         m_strSummary;                   //保存主题
    QDateTime       m_dateTime;                     //保存推送时间
    QString         m_strBody;                      //保存正文

    uint            m_uNotifyTime;                  //保存推送时间的绝对时间

    QTimer*         m_pSetJumpDelayTimer;           //跳转定时器
    QString         m_strUrl;                       //跳转链接
    QString         m_strAction;                    //跳转动作

    QPoint          m_pressedPos;                   //按下位置
    bool            m_everMoved = false;
    bool            m_bFold= true;                  //是否折叠,默认折叠状态
    bool            m_bMain = true;                 //是否为主窗口,默认是主窗口
    int             m_nShowLeftCount;               //为主窗口时,剩余显示条数
    bool            m_bTimeFormat = true;           //time制式，false代表12小时制，true代表24小时制. 默认24小时制
    bool            jumpFlag;                       //跳转标志

signals:
    void            Sig_onDeleSingleMsg(SingleMsg* p); //删除单条消息信号
    void            Sig_onDeleteAppMsg();              //删除单个应用集合的消息信号
    void            Sig_setAppFoldFlag(bool bFoldFlag);//折叠消息信号

    void            Sig_notifyAppShowBaseMap();     //当动画折叠后，通知主app消息显示底图
    void            Sig_notifyAppHideBaseMap();     //当动画展开后，通知主app消息隐藏底图
    void            Sig_jumpAction();               //执行跳转动作信号

public slots:
    void            onDele();                                       //通知中心消息删除
    void            updateUnfoldMove(const QVariant &value);        //更新展开的移动数据
    void            onUnfoldFinish();                               //处理展开完成时的函数
    void            updateFoldMove(const QVariant &value);          //更新折叠的移动数据
    void            onFoldFinish();                                 //处理折叠完成时的函数
    void            updateDeleLeftMove(const QVariant &value);      //更新删除左移时的移动数据
    void            onDeleLeftMoveFinish();                         //处理删除左移完成时的函数
    void            updateDeleUpperMove(const QVariant &value);     //更新删除上移时的移动数据
    void            onDeleUpperMoveFinish();                        //处理删除上移完成时的函数
    void            startAnimationDeleLeftMove();                   //开启删除左移动画
    void            updateLabelAlphaF();
    void            onUpdateSinglemsgWidget(QString key);
    void            onButtonStyle();                                //更新按鈕样式
    void            styleGsettingChanged(QString key);

};


class SingleWidget : public QWidget
{
    Q_OBJECT
public:
    explicit SingleWidget(QWidget *parent = nullptr);
    bool m_bFold = false;

private:
    enum TaskWidgetStatus{NORMAL, HOVER, PRESS};      //枚举三态
    TaskWidgetStatus  status;                         //保存当前三态状态

    virtual void enterEvent(QEvent *event) override;
    virtual void leaveEvent(QEvent *event) override;
    virtual void mouseReleaseEvent(QMouseEvent *event) override;
    virtual void paintEvent(QPaintEvent *e) override;

};

#endif // SINGLEMSG_H
